#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stack>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
ll H[N];
ll V[N];
ll sum[N];
int n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> H[i] >> V[i];
    }
    stack<ll> as;
    for (int i = 1; i <= n; i++)
    {
        while (as.size() && H[as.top()] <= H[i])
        {
            as.pop();
        }
        if (as.size())
        {
            sum[as.top()] += V[i];
        }
        as.push(i);
    }
    while (as.size()) as.pop();
    for (int i = n; i >= 1; i--)
    {
        while (as.size() && H[as.top()] <= H[i])
        {
            as.pop();
        }
        if (as.size())
        {
            sum[as.top()] += V[i];
        }
        as.push(i);
    }
    ll ret = 0;
    for (int i = 1; i <= n; i++)
    {
        ret = max(ret, sum[i]);
    }
    cout << ret;
    return 0;
}